<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE Trustworthy #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE CPP
           , NoImplicitPrelude
           , NondecreasingIndentation
  #-}</span><span>
</span><span id="line-6"></span><span class="hs-pragma">{-# OPTIONS_HADDOCK not-home #-}</span><span>
</span><span id="line-7"></span><span>
</span><span id="line-8"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- Module      :  GHC.IO.Encoding.Iconv</span><span>
</span><span id="line-11"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow, 2008-2009</span><span>
</span><span id="line-12"></span><span class="hs-comment">-- License     :  see libraries/base/LICENSE</span><span>
</span><span id="line-13"></span><span class="hs-comment">--</span><span>
</span><span id="line-14"></span><span class="hs-comment">-- Maintainer  :  libraries@haskell.org</span><span>
</span><span id="line-15"></span><span class="hs-comment">-- Stability   :  internal</span><span>
</span><span id="line-16"></span><span class="hs-comment">-- Portability :  non-portable</span><span>
</span><span id="line-17"></span><span class="hs-comment">--</span><span>
</span><span id="line-18"></span><span class="hs-comment">-- This module provides text encoding/decoding using iconv</span><span>
</span><span id="line-19"></span><span class="hs-comment">--</span><span>
</span><span id="line-20"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-21"></span><span>
</span><span id="line-22"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">GHC.IO.Encoding.Iconv</span><span> </span><span class="hs-special">(</span><span class="hs-cpp">
#if !defined(mingw32_HOST_OS)
</span><span>   </span><span class="hs-identifier">iconvEncoding</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">mkIconvEncoding</span><span class="hs-special">,</span><span>
</span><span id="line-25"></span><span>   </span><span class="hs-identifier">localeEncodingName</span><span class="hs-cpp">
#endif
</span><span> </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span class="hs-cpp">

#include &quot;MachDeps.h&quot;
</span><span class="hs-cpp">#include &quot;HsBaseConfig.h&quot;
</span><span class="hs-cpp">
#if defined(mingw32_HOST_OS)
</span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Base.html"><span class="hs-identifier">GHC.Base</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-comment">-- For build ordering</span><span class="hs-cpp">
#else
</span><span>
</span><span id="line-36"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Foreign</span><span>
</span><span id="line-37"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Foreign.C</span><span> </span><span class="hs-keyword">hiding</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">charIsRepresentable</span><span class="hs-special">)</span><span>
</span><span id="line-38"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Data.Maybe</span><span>
</span><span id="line-39"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Base</span><span>
</span><span id="line-40"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Foreign</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">charIsRepresentable</span><span class="hs-special">)</span><span>
</span><span id="line-41"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO.Buffer</span><span>
</span><span id="line-42"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO.Encoding.Failure</span><span>
</span><span id="line-43"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO.Encoding.Types</span><span>
</span><span id="line-44"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.List</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">span</span><span class="hs-special">)</span><span>
</span><span id="line-45"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Num</span><span>
</span><span id="line-46"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Show</span><span>
</span><span id="line-47"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Real</span><span>
</span><span id="line-48"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">System.IO.Unsafe</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">unsafePerformIO</span><span class="hs-special">)</span><span>
</span><span id="line-49"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">System.Posix.Internals</span><span>
</span><span id="line-50"></span><span>
</span><span id="line-51"></span><span class="hs-identifier">c_DEBUG_DUMP</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Bool</span><span>
</span><span id="line-52"></span><span class="hs-identifier">c_DEBUG_DUMP</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">False</span><span>
</span><span id="line-53"></span><span>
</span><span id="line-54"></span><span class="hs-identifier">iconv_trace</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">String</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-55"></span><span class="hs-identifier">iconv_trace</span><span> </span><span class="hs-identifier">s</span><span>
</span><span id="line-56"></span><span> </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">c_DEBUG_DUMP</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">puts</span><span> </span><span class="hs-identifier">s</span><span>
</span><span id="line-57"></span><span> </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-58"></span><span>
</span><span id="line-59"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-60"></span><span class="hs-comment">-- iconv encoders/decoders</span><span>
</span><span id="line-61"></span><span>
</span><span id="line-62"></span><span class="hs-pragma">{-# NOINLINE</span><span> </span><span class="hs-pragma">localeEncodingName</span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-63"></span><span class="hs-identifier">localeEncodingName</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">String</span><span>
</span><span id="line-64"></span><span class="hs-identifier">localeEncodingName</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">unsafePerformIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-65"></span><span>   </span><span class="hs-comment">-- Use locale_charset() or nl_langinfo(CODESET) to get the encoding</span><span>
</span><span id="line-66"></span><span>   </span><span class="hs-comment">-- if we have either of them.</span><span>
</span><span id="line-67"></span><span>   </span><span class="hs-identifier">cstr</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">c_localeEncoding</span><span>
</span><span id="line-68"></span><span>   </span><span class="hs-identifier">peekCAString</span><span> </span><span class="hs-identifier">cstr</span><span> </span><span class="hs-comment">-- Assume charset names are ASCII</span><span>
</span><span id="line-69"></span><span>
</span><span id="line-70"></span><span class="hs-comment">-- We hope iconv_t is a storable type.  It should be, since it has at least the</span><span>
</span><span id="line-71"></span><span class="hs-comment">-- value -1, which is a possible return value from iconv_open.</span><span>
</span><span id="line-72"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">IConv</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">CLong</span><span> </span><span class="hs-comment">-- ToDo: (#type iconv_t)</span><span>
</span><span id="line-73"></span><span>
</span><span id="line-74"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">unsafe</span><span> </span><span class="hs-string">&quot;hs_iconv_open&quot;</span><span>
</span><span id="line-75"></span><span>    </span><span class="hs-identifier">hs_iconv_open</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">CString</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">CString</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">IConv</span><span>
</span><span id="line-76"></span><span>
</span><span id="line-77"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">unsafe</span><span> </span><span class="hs-string">&quot;hs_iconv_close&quot;</span><span>
</span><span id="line-78"></span><span>    </span><span class="hs-identifier">hs_iconv_close</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IConv</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">CInt</span><span>
</span><span id="line-79"></span><span>
</span><span id="line-80"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">unsafe</span><span> </span><span class="hs-string">&quot;hs_iconv&quot;</span><span>
</span><span id="line-81"></span><span>    </span><span class="hs-identifier">hs_iconv</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IConv</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">CString</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">CSize</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">CString</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">CSize</span><span>
</span><span id="line-82"></span><span>          </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">CSize</span><span>
</span><span id="line-83"></span><span>
</span><span id="line-84"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">unsafe</span><span> </span><span class="hs-string">&quot;localeEncoding&quot;</span><span>
</span><span id="line-85"></span><span>    </span><span class="hs-identifier">c_localeEncoding</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">CString</span><span>
</span><span id="line-86"></span><span>
</span><span id="line-87"></span><span class="hs-identifier">haskellChar</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">String</span><span class="hs-cpp">
#if defined(WORDS_BIGENDIAN)
</span><span class="hs-identifier">haskellChar</span><span> </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">charSize</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">2</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-string">&quot;UTF-16BE&quot;</span><span>
</span><span id="line-90"></span><span>            </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-string">&quot;UTF-32BE&quot;</span><span class="hs-cpp">
#else
</span><span class="hs-identifier">haskellChar</span><span> </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">charSize</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">2</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-string">&quot;UTF-16LE&quot;</span><span>
</span><span id="line-93"></span><span>            </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-string">&quot;UTF-32LE&quot;</span><span class="hs-cpp">
#endif
</span><span>
</span><span id="line-96"></span><span class="hs-identifier">char_shift</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-97"></span><span class="hs-identifier">char_shift</span><span> </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">charSize</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">2</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">1</span><span>
</span><span id="line-98"></span><span>           </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">2</span><span>
</span><span id="line-99"></span><span>
</span><span id="line-100"></span><span class="hs-identifier">iconvEncoding</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">String</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Maybe</span><span> </span><span class="hs-identifier">TextEncoding</span><span class="hs-special">)</span><span>
</span><span id="line-101"></span><span class="hs-identifier">iconvEncoding</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">mkIconvEncoding</span><span> </span><span class="hs-identifier">ErrorOnCodingFailure</span><span>
</span><span id="line-102"></span><span>
</span><span id="line-103"></span><span class="hs-comment">-- | Construct an iconv-based 'TextEncoding' for the given character set and</span><span>
</span><span id="line-104"></span><span class="hs-comment">-- 'CodingFailureMode'.</span><span>
</span><span id="line-105"></span><span class="hs-comment">--</span><span>
</span><span id="line-106"></span><span class="hs-comment">-- As iconv is missing in some minimal environments (e.g. #10298), this</span><span>
</span><span id="line-107"></span><span class="hs-comment">-- checks to ensure that iconv is working properly before returning the</span><span>
</span><span id="line-108"></span><span class="hs-comment">-- encoding, returning 'Nothing' if not.</span><span>
</span><span id="line-109"></span><span class="hs-identifier">mkIconvEncoding</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">CodingFailureMode</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">String</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Maybe</span><span> </span><span class="hs-identifier">TextEncoding</span><span class="hs-special">)</span><span>
</span><span id="line-110"></span><span class="hs-identifier">mkIconvEncoding</span><span> </span><span class="hs-identifier">cfm</span><span> </span><span class="hs-identifier">charset</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-111"></span><span>    </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">enc</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">TextEncoding</span><span> </span><span class="hs-special">{</span><span>
</span><span id="line-112"></span><span>                  </span><span class="hs-identifier">textEncodingName</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">charset</span><span class="hs-special">,</span><span>
</span><span id="line-113"></span><span>                  </span><span class="hs-identifier">mkTextDecoder</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">newIConv</span><span> </span><span class="hs-identifier">raw_charset</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">haskellChar</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">suffix</span><span class="hs-special">)</span><span>
</span><span id="line-114"></span><span>                                           </span><span class="hs-special">(</span><span class="hs-identifier">recoverDecode</span><span> </span><span class="hs-identifier">cfm</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">iconvDecode</span><span class="hs-special">,</span><span>
</span><span id="line-115"></span><span>                  </span><span class="hs-identifier">mkTextEncoder</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">newIConv</span><span> </span><span class="hs-identifier">haskellChar</span><span> </span><span class="hs-identifier">charset</span><span>
</span><span id="line-116"></span><span>                                           </span><span class="hs-special">(</span><span class="hs-identifier">recoverEncode</span><span> </span><span class="hs-identifier">cfm</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">iconvEncode</span><span class="hs-special">}</span><span>
</span><span id="line-117"></span><span>    </span><span class="hs-identifier">good</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">charIsRepresentable</span><span> </span><span class="hs-identifier">enc</span><span> </span><span class="hs-char">'a'</span><span>
</span><span id="line-118"></span><span>    </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">good</span><span>
</span><span id="line-119"></span><span>               </span><span class="hs-keyword">then</span><span> </span><span class="hs-identifier">Just</span><span> </span><span class="hs-identifier">enc</span><span>
</span><span id="line-120"></span><span>               </span><span class="hs-keyword">else</span><span> </span><span class="hs-identifier">Nothing</span><span>
</span><span id="line-121"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-122"></span><span>    </span><span class="hs-comment">-- An annoying feature of GNU iconv is that the //PREFIXES only take</span><span>
</span><span id="line-123"></span><span>    </span><span class="hs-comment">-- effect when they appear on the tocode parameter to iconv_open:</span><span>
</span><span id="line-124"></span><span>    </span><span class="hs-special">(</span><span class="hs-identifier">raw_charset</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">suffix</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">span</span><span> </span><span class="hs-special">(</span><span class="hs-operator">/=</span><span> </span><span class="hs-char">'/'</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">charset</span><span>
</span><span id="line-125"></span><span>
</span><span id="line-126"></span><span class="hs-identifier">newIConv</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">String</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">String</span><span>
</span><span id="line-127"></span><span>   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">b</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">a</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">b</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-128"></span><span>   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">IConv</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">b</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">CodingProgress</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">a</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">b</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-129"></span><span>   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">BufferCodec</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-identifier">b</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-130"></span><span class="hs-identifier">newIConv</span><span> </span><span class="hs-identifier">from</span><span> </span><span class="hs-identifier">to</span><span> </span><span class="hs-identifier">rec</span><span> </span><span class="hs-identifier">fn</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-131"></span><span>  </span><span class="hs-comment">-- Assume charset names are ASCII</span><span>
</span><span id="line-132"></span><span>  </span><span class="hs-identifier">withCAString</span><span> </span><span class="hs-identifier">from</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span> </span><span class="hs-identifier">from_str</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-133"></span><span>  </span><span class="hs-identifier">withCAString</span><span> </span><span class="hs-identifier">to</span><span>   </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span> </span><span class="hs-identifier">to_str</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-134"></span><span>    </span><span class="hs-identifier">iconvt</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">throwErrnoIfMinus1</span><span> </span><span class="hs-string">&quot;mkTextEncoding&quot;</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">hs_iconv_open</span><span> </span><span class="hs-identifier">to_str</span><span> </span><span class="hs-identifier">from_str</span><span>
</span><span id="line-135"></span><span>    </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">iclose</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">throwErrnoIfMinus1_</span><span> </span><span class="hs-string">&quot;Iconv.close&quot;</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">hs_iconv_close</span><span> </span><span class="hs-identifier">iconvt</span><span>
</span><span id="line-136"></span><span>    </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">BufferCodec</span><span class="hs-special">{</span><span>
</span><span id="line-137"></span><span>                </span><span class="hs-identifier">encode</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">fn</span><span> </span><span class="hs-identifier">iconvt</span><span class="hs-special">,</span><span>
</span><span id="line-138"></span><span>                </span><span class="hs-identifier">recover</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">rec</span><span class="hs-special">,</span><span>
</span><span id="line-139"></span><span>                </span><span class="hs-identifier">close</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">iclose</span><span class="hs-special">,</span><span>
</span><span id="line-140"></span><span>                </span><span class="hs-comment">-- iconv doesn't supply a way to save/restore the state</span><span>
</span><span id="line-141"></span><span>                </span><span class="hs-identifier">getState</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-142"></span><span>                </span><span class="hs-identifier">setState</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">const</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-143"></span><span>                </span><span class="hs-special">}</span><span>
</span><span id="line-144"></span><span>
</span><span id="line-145"></span><span class="hs-identifier">iconvDecode</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IConv</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DecodeBuffer</span><span>
</span><span id="line-146"></span><span class="hs-identifier">iconvDecode</span><span> </span><span class="hs-identifier">iconv_t</span><span> </span><span class="hs-identifier">ibuf</span><span> </span><span class="hs-identifier">obuf</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">iconvRecode</span><span> </span><span class="hs-identifier">iconv_t</span><span> </span><span class="hs-identifier">ibuf</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-identifier">obuf</span><span> </span><span class="hs-identifier">char_shift</span><span>
</span><span id="line-147"></span><span>
</span><span id="line-148"></span><span class="hs-identifier">iconvEncode</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IConv</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">EncodeBuffer</span><span>
</span><span id="line-149"></span><span class="hs-identifier">iconvEncode</span><span> </span><span class="hs-identifier">iconv_t</span><span> </span><span class="hs-identifier">ibuf</span><span> </span><span class="hs-identifier">obuf</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">iconvRecode</span><span> </span><span class="hs-identifier">iconv_t</span><span> </span><span class="hs-identifier">ibuf</span><span> </span><span class="hs-identifier">char_shift</span><span> </span><span class="hs-identifier">obuf</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-150"></span><span>
</span><span id="line-151"></span><span class="hs-identifier">iconvRecode</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IConv</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Int</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">b</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-152"></span><span>            </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">CodingProgress</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">a</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">b</span><span class="hs-special">)</span><span>
</span><span id="line-153"></span><span class="hs-identifier">iconvRecode</span><span> </span><span class="hs-identifier">iconv_t</span><span>
</span><span id="line-154"></span><span>  </span><span class="hs-identifier">input</span><span class="hs-glyph">@</span><span class="hs-identifier">Buffer</span><span class="hs-special">{</span><span>  </span><span class="hs-identifier">bufRaw</span><span class="hs-glyph">=</span><span class="hs-identifier">iraw</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">bufL</span><span class="hs-glyph">=</span><span class="hs-identifier">ir</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">bufR</span><span class="hs-glyph">=</span><span class="hs-identifier">iw</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">bufSize</span><span class="hs-glyph">=</span><span class="hs-identifier">_</span><span>  </span><span class="hs-special">}</span><span>  </span><span class="hs-identifier">iscale</span><span>
</span><span id="line-155"></span><span>  </span><span class="hs-identifier">output</span><span class="hs-glyph">@</span><span class="hs-identifier">Buffer</span><span class="hs-special">{</span><span> </span><span class="hs-identifier">bufRaw</span><span class="hs-glyph">=</span><span class="hs-identifier">oraw</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">bufL</span><span class="hs-glyph">=</span><span class="hs-identifier">_</span><span class="hs-special">,</span><span>  </span><span class="hs-identifier">bufR</span><span class="hs-glyph">=</span><span class="hs-identifier">ow</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">bufSize</span><span class="hs-glyph">=</span><span class="hs-identifier">os</span><span> </span><span class="hs-special">}</span><span>  </span><span class="hs-identifier">oscale</span><span>
</span><span id="line-156"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-157"></span><span>    </span><span class="hs-identifier">iconv_trace</span><span> </span><span class="hs-special">(</span><span class="hs-string">&quot;haskellChar=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-identifier">haskellChar</span><span class="hs-special">)</span><span>
</span><span id="line-158"></span><span>    </span><span class="hs-identifier">iconv_trace</span><span> </span><span class="hs-special">(</span><span class="hs-string">&quot;iconvRecode before, input=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">summaryBuffer</span><span> </span><span class="hs-identifier">input</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-159"></span><span>    </span><span class="hs-identifier">iconv_trace</span><span> </span><span class="hs-special">(</span><span class="hs-string">&quot;iconvRecode before, output=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">summaryBuffer</span><span> </span><span class="hs-identifier">output</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-160"></span><span>    </span><span class="hs-identifier">withRawBuffer</span><span> </span><span class="hs-identifier">iraw</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span> </span><span class="hs-identifier">piraw</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-161"></span><span>    </span><span class="hs-identifier">withRawBuffer</span><span> </span><span class="hs-identifier">oraw</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span> </span><span class="hs-identifier">poraw</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-162"></span><span>    </span><span class="hs-identifier">with</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">piraw</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">plusPtr</span><span class="hs-special">`</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">ir</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">shiftL</span><span class="hs-special">`</span><span> </span><span class="hs-identifier">iscale</span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span> </span><span class="hs-identifier">p_inbuf</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-163"></span><span>    </span><span class="hs-identifier">with</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">poraw</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">plusPtr</span><span class="hs-special">`</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">ow</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">shiftL</span><span class="hs-special">`</span><span> </span><span class="hs-identifier">oscale</span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span> </span><span class="hs-identifier">p_outbuf</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-164"></span><span>    </span><span class="hs-identifier">with</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-identifier">iw</span><span class="hs-glyph">-</span><span class="hs-identifier">ir</span><span class="hs-special">)</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">shiftL</span><span class="hs-special">`</span><span> </span><span class="hs-identifier">iscale</span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span> </span><span class="hs-identifier">p_inleft</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-165"></span><span>    </span><span class="hs-identifier">with</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-identifier">os</span><span class="hs-glyph">-</span><span class="hs-identifier">ow</span><span class="hs-special">)</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">shiftL</span><span class="hs-special">`</span><span> </span><span class="hs-identifier">oscale</span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span> </span><span class="hs-identifier">p_outleft</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-166"></span><span>      </span><span class="hs-identifier">res</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">hs_iconv</span><span> </span><span class="hs-identifier">iconv_t</span><span> </span><span class="hs-identifier">p_inbuf</span><span> </span><span class="hs-identifier">p_inleft</span><span> </span><span class="hs-identifier">p_outbuf</span><span> </span><span class="hs-identifier">p_outleft</span><span>
</span><span id="line-167"></span><span>      </span><span class="hs-identifier">new_inleft</span><span>  </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">p_inleft</span><span>
</span><span id="line-168"></span><span>      </span><span class="hs-identifier">new_outleft</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">p_outleft</span><span>
</span><span id="line-169"></span><span>      </span><span class="hs-keyword">let</span><span>
</span><span id="line-170"></span><span>          </span><span class="hs-identifier">new_inleft'</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">new_inleft</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">shiftR</span><span class="hs-special">`</span><span> </span><span class="hs-identifier">iscale</span><span>
</span><span id="line-171"></span><span>          </span><span class="hs-identifier">new_outleft'</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">new_outleft</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">shiftR</span><span class="hs-special">`</span><span> </span><span class="hs-identifier">oscale</span><span>
</span><span id="line-172"></span><span>          </span><span class="hs-identifier">new_input</span><span>
</span><span id="line-173"></span><span>            </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">new_inleft</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">0</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">input</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-identifier">bufL</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">0</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">bufR</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-174"></span><span>            </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span>        </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">input</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-identifier">bufL</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">iw</span><span> </span><span class="hs-glyph">-</span><span> </span><span class="hs-identifier">new_inleft'</span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-175"></span><span>          </span><span class="hs-identifier">new_output</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">output</span><span class="hs-special">{</span><span> </span><span class="hs-identifier">bufR</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">os</span><span> </span><span class="hs-glyph">-</span><span> </span><span class="hs-identifier">new_outleft'</span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-176"></span><span>      </span><span class="hs-identifier">iconv_trace</span><span> </span><span class="hs-special">(</span><span class="hs-string">&quot;iconv res=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-identifier">res</span><span class="hs-special">)</span><span>
</span><span id="line-177"></span><span>      </span><span class="hs-identifier">iconv_trace</span><span> </span><span class="hs-special">(</span><span class="hs-string">&quot;iconvRecode after,  input=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">summaryBuffer</span><span> </span><span class="hs-identifier">new_input</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-178"></span><span>      </span><span class="hs-identifier">iconv_trace</span><span> </span><span class="hs-special">(</span><span class="hs-string">&quot;iconvRecode after,  output=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">summaryBuffer</span><span> </span><span class="hs-identifier">new_output</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-179"></span><span>      </span><span class="hs-keyword">if</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">res</span><span> </span><span class="hs-operator">/=</span><span> </span><span class="hs-glyph">-</span><span class="hs-number">1</span><span class="hs-special">)</span><span>
</span><span id="line-180"></span><span>        </span><span class="hs-keyword">then</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-comment">-- all input translated</span><span>
</span><span id="line-181"></span><span>           </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">InputUnderflow</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">new_input</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">new_output</span><span class="hs-special">)</span><span>
</span><span id="line-182"></span><span>        </span><span class="hs-keyword">else</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-183"></span><span>      </span><span class="hs-identifier">errno</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">getErrno</span><span>
</span><span id="line-184"></span><span>      </span><span class="hs-keyword">case</span><span> </span><span class="hs-identifier">errno</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-185"></span><span>        </span><span class="hs-identifier">e</span><span> </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">e</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">e2BIG</span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">OutputUnderflow</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">new_input</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">new_output</span><span class="hs-special">)</span><span>
</span><span id="line-186"></span><span>          </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">e</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">eINVAL</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">InputUnderflow</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">new_input</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">new_output</span><span class="hs-special">)</span><span>
</span><span id="line-187"></span><span>           </span><span class="hs-comment">-- Sometimes iconv reports EILSEQ for a</span><span>
</span><span id="line-188"></span><span>           </span><span class="hs-comment">-- character in the input even when there is no room</span><span>
</span><span id="line-189"></span><span>           </span><span class="hs-comment">-- in the output; in this case we might be about to</span><span>
</span><span id="line-190"></span><span>           </span><span class="hs-comment">-- change the encoding anyway, so the following bytes</span><span>
</span><span id="line-191"></span><span>           </span><span class="hs-comment">-- could very well be in a different encoding.</span><span>
</span><span id="line-192"></span><span>           </span><span class="hs-comment">--</span><span>
</span><span id="line-193"></span><span>           </span><span class="hs-comment">-- Because we can only say InvalidSequence if there is at least</span><span>
</span><span id="line-194"></span><span>           </span><span class="hs-comment">-- one element left in the output, we have to special case this.</span><span>
</span><span id="line-195"></span><span>          </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">e</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">eILSEQ</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">new_outleft'</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-keyword">then</span><span> </span><span class="hs-identifier">OutputUnderflow</span><span> </span><span class="hs-keyword">else</span><span> </span><span class="hs-identifier">InvalidSequence</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">new_input</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">new_output</span><span class="hs-special">)</span><span>
</span><span id="line-196"></span><span>          </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-197"></span><span>              </span><span class="hs-identifier">iconv_trace</span><span> </span><span class="hs-special">(</span><span class="hs-string">&quot;iconv returned error: &quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">errnoToIOError</span><span> </span><span class="hs-string">&quot;iconv&quot;</span><span> </span><span class="hs-identifier">e</span><span> </span><span class="hs-identifier">Nothing</span><span> </span><span class="hs-identifier">Nothing</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-198"></span><span>              </span><span class="hs-identifier">throwErrno</span><span> </span><span class="hs-string">&quot;iconvRecoder&quot;</span><span class="hs-cpp">

#endif /* !mingw32_HOST_OS */
</span><span>
</span><span id="line-202"></span></pre></body></html>